package com.sinvo.oa.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinvo.oa.entity.model.system.SysPost;
import com.sinvo.oa.entity.model.system.SysRole;
import com.sinvo.oa.entity.vo.system.SysPostQueryVo;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.log.annotation.Log;
import com.sinvo.oa.log.enums.ServiceType;
import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 岗位控制器
 */
@Api(tags = "岗位管理接口")
@RestController
@RequestMapping("admin/system/sysPost")
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;

    /**
     * 查询所有岗位 用于下拉框
     * @return R
     */
    @ApiOperation(value = "查询所有岗位")
    @GetMapping("findAll")
    public R findAll() {
        //封装条件 状态为正常的
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPost::getStatus,1);
        List<SysPost> postList = sysPostService.list(queryWrapper);
        return R.success(postList);
    }

    @PreAuthorize("hasAuthority('btn.sysPost.list')")
    @ApiOperation(value = "条件分页查询")
    @GetMapping("{pageNum}/{pageSize}")
    public R pageQueryPost(@PathVariable Long pageNum,
                           @PathVariable Long pageSize,
                           SysPostQueryVo sysPostQueryVo){
        //pageNum 当前页  pageSize 每页显示记录数
        Page<SysPost> page = new Page<>(pageNum, pageSize);
        page = sysPostService.findPostByPage(page,sysPostQueryVo);
        return R.success(page);
    }

    @Log(title = "岗位管理",businessType = ServiceType.INSERT)
    @PreAuthorize("hasAuthority('btn.sysPost.add')")
    @ApiOperation(value = "添加岗位")
    @PostMapping("save")
    public R save(@Validated @RequestBody SysPost sysPost, BindingResult result){
        //参数校验
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        sysPostService.savePost(sysPost);
        return R.success();
    }

    @Log(title = "岗位管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysPost.update')")
    @ApiOperation(value = "修改岗位")
    @PutMapping("update")
    public R update(@Validated @RequestBody SysPost sysPost,BindingResult result){
        //参数校验
        if (result.hasErrors()) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        sysPostService.updatePost(sysPost);
        return R.success();
    }

    @Log(title = "岗位管理",businessType = ServiceType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysPost.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        boolean success = sysPostService.removeById(id);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "删除失败，请重试");
        }
        return R.success();
    }

    @PreAuthorize("hasAuthority('btn.sysPost.list')")
    @ApiOperation(value = "根据id查询")
    @GetMapping("get/{id}")
    public R get(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(),"非法参数");
        }
        SysPost sysPost = sysPostService.getById(id);
        return R.success(sysPost);
    }

    @Log(title = "岗位管理",businessType = ServiceType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysMenu.update')")
    @ApiOperation(value = "更新菜单状态")
    @GetMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        //验证参数 id为空或者status不等于1或0 提示错误
        if (StringUtils.isEmpty(id) || !(status == 1 || status == 0)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        boolean success = sysPostService.changeStatus(id, status);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "修改失败，请重试");
        }
        return R.success();
    }
}

